<template>
  <!-- 角色分配 -->
  <div class="role-assignment-wrap assignment-wrap">
    <div class="role-title">{{ $t("module.userManage.roleAssignment") }}</div>
    <InnerPageTable :config="tableConfig" class="dialog-cancel-tablestyle"></InnerPageTable>
  </div>
</template>

<script>
import locales from "./locales.js";
import userManageAPI from "@/api/userManageApi";
export default {
  name: "RoleAssignment",
  mixins: [locales],
  props: {
    userId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      tableConfig: {
        resource: null, // 数据源
        columns: [
          {
            title: this.$t("module.userManage.roleName"),
            key: "roleName"
          },
          {
            title: this.$t("module.userManage.orgName"),
            key: "orgName"
          }
        ], // 表格列配置
        autoFirst: false, // 渲染第一页数据
        isPage: false,
        thresholdValue: 230
      }
    };
  },
  created() {
    // 获取角色分配数据
    this.getRoleAssignmentInfo();
  },
  methods: {
    // 编辑用户时，获取角色分配表格数据
    getRoleAssignmentInfo() {
      userManageAPI.findOrgListByUserId(this.userId).then(({ status, data }) => {
        if (status === 200 && data && data.length) {
          let obj = {};
          let arr = data.reduce((prev, cur) => {
            obj[cur.roleCode] ? "" : (obj[cur.roleCode] = true && prev.push(cur));
            return prev;
          }, []);

          let result = [];
          for (let i = 0; i < data.length; i++) {
            for (let j = 0; j < arr.length; j++) {
              if (data[i].roleCode === arr[j].roleCode) {
                arr[j].orgName += ";" + data[i].orgName;
              }
            }
          }

          let tableData = [];
          arr.forEach((item, index) => {
            item.orgName.split(";").splice(1);
            tableData.push(item.orgName.split(";").splice(1));
            arr[index].orgName = item.orgName
              .split(";")
              .splice(1)
              .join(";");
          });
          this.tableConfig.resource = arr;
        }
      });
    }
  }
};
</script>

<style lang="less" scoped>
// 角色分配

.assignment-wrap {
  .inner-table {
    min-height: 120px !important;
  }
  /deep/.ivu-table-wrapper {
    .ivu-table-body{
      overflow: hidden;
      max-height: 144px !important;
      overflow-y: auto;
    }
  }
  .table-bar {
    margin-bottom: -20px;
  }
  // .ivu-table-overflowY {
  //   max-height: 104px !important;
  // }
  .role-title {
    border-bottom: 1px solid #d8d8d8;
    font-size: 14px;
    font-weight: 700;
    line-height: 40px;
  }
  .role-table {
    margin-top: 20px;
  }
  .dialog-cancel-tablestyle {
    .table-bar {
      margin: 0;
    }
    .single-table-con {
      padding: 0;
    }
  }
}
</style>
